package com.tz.crm.service.impl;

import com.tz.crm.dao.BaseDao;
import com.tz.crm.service.BaseService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * Service实现类 - Service实现类基类
 *
 * @param <T>  实体类泛型
 * @param <PK> 主键泛型
 */
@Service
@Transactional
public class BaseServiceImpl<T, PK extends Serializable> implements BaseService<T, PK> {

    private static final Logger LOGGER = Logger.getLogger(BaseServiceImpl.class);

    private BaseDao<T, PK> baseDao;

    public BaseDao<T, PK> getBaseDao() {
        return baseDao;
    }

    public void setBaseDao(BaseDao<T, PK> baseDao) {
        this.baseDao = baseDao;
    }

    @Override
    public T get(PK id) {
        return baseDao.get(id);
    }

    @Override
    public List<T> get(PK[] ids) {
        return baseDao.get(ids);
    }

    @Override
    public T get(String propertyName, Object value) {
        return baseDao.get(propertyName, value);
    }

    @Override
    public List<T> getList(String propertyName, Object value) {
        return baseDao.getList(propertyName, value);
    }

    @Override
    public List<T> getAll() {
        return baseDao.getAll();
    }

    @Override
    public Long getTotalCount() {
        return baseDao.getTotalCount();
    }

    @Override
    public boolean isExist(String propertyName, Object value) {
        return baseDao.isExist(propertyName, value);
    }

    @Override
    public PK save(T entity) {
        if (baseDao != null) {
            return baseDao.save(entity);
        } else {
            LOGGER.fatal("注入失败");
            return null;
        }

    }

    @Override
    public void update(T entity) {
        baseDao.update(entity);
    }

    @Override
    public void delete(T entity) {
        baseDao.delete(entity);
    }

    @Override
    public void delete(PK id) {
        baseDao.delete(id);
    }

    @Override
    public void delete(PK[] ids) {
        baseDao.delete(ids);
    }

}
